<?
include("session.php"); 
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="../../../webkit/utils/jquery.js"></script>
<script type="text/javascript" src="../../../webkit/utils/form.checker.js"></script>
<script type="text/javascript" src="../../../webkit/datepicker/WdatePicker.js"></script>
<link type="text/css" rel="stylesheet" href="../../page.css"/> 
<body  bgcolor="#f4f4f4" style="margin:0px;" scroll=no>
<style> .input {width:150px} </style>
<hr size=1>

<table width="200px" cellspacing=1 cellpadding=3 id="basic_info_table"> 
<form name=f> 
	<tr>
		<td align=right nowrap>任务名称</td><td><input type='text' id="task_name" class='input' readonly style="color:blue"></td>  
		<td align=right nowrap>　　可分配条数</td><td><input type='text' id="free_count" class='input' readonly style="color:blue"></td>
	</tr>
	<tr>
		<td align=right nowrap>　　业务分组</td><td><select id="agent_group" class='input' onchange="agent_group_change(this.value)"><option value=''>-请选择-</option></select></td>  
		<td align=right nowrap>　　要分配条数</td><td><input type='text' id="dispatch_count" class='input' style="color:red" value=""></td>
	</tr>
	<tr>
		<td align=right valign=top>座席工号<br>[<a href="javascript:select_all_user(true)">全　选</a>]<br>[<a href="javascript:select_all_user(false)">清　空</a>]</td><td colspan=3 id="agent_td">
			<font color=gray>请选择业务分组</font>
			<!--
			<input type="checkbox" value="hncsgj01" checked> hncsgj01　
			<input type="checkbox" value="hncsgj02" checked> hncsgj02　
			<input type="checkbox" value="hncsgj03" checked> hncsgj03　
			<input type="checkbox" value="hncsgj04" checked> hncsgj04<br>
			<input type="checkbox" value="hncsgj05" checked> hncsgj05<br>
			-->
		</td>
	</tr>
</form>	
</table>

<table width=100% cellspacing=1 cellpadding=3>
	<tr>
		<td align=center>
		<hr size=1>
		<input type="button" id="btn_save" class="button" value="确定分配" onclick="dispatch()" disabled>　　<input id="btn_close" type="button" class="button" value="关闭窗口" onclick="parent.closeDlg()" disabled></td>
	</tr>
</table>

<script type="text/javascript">
	var f = document.f;
	var task_id = '<?echo $_GET['task_id']?>';

	function dispatch() { 
		var agent_ids = []
		$("input[type='checkbox']").each(
			function(i) {
				if($(this).attr('checked')) {
					agent_ids.push($(this).val())
				}
			}
		);
		if(agent_ids.length == 0) {
			alert('请选择至少一个座席工号！')
			return false;
		}

		var dispatch_count = parseInt($("#dispatch_count").val());
		var free_count = parseInt($("#free_count").val());
		if(free_count == 0) {
			alert("当前没有可分配的记录条数！");
			return false;
		}
		if(dispatch_count > free_count) {
			alert("当前可分配条数最多只有[ "+free_count+" ]条！");
			return false;
		}


		var params = {'task_id':task_id, 'agent_ids':agent_ids.join(","),'dispatch_count':dispatch_count};

		if(confirm('确定要保存任务分配信息吗？')) {
			$('#btn_save').attr('disabled', true);
			$('#btn_save').val('保存中..');
			$.post("task_query.php?action=dispatch", params, function(data) {
				if(data.indexOf("ok") == 0) {
					alert("数据保存成功!");
					parent.refreshGrid();
					parent.closeDlg();
				} else if(data == "no record") {
					alert('请先导入号码数据，再进行分配操作！')
				} else {  
					alert(data);
				}
				$('#btn_save').attr('disabled', false);
				$('#btn_save').val('保存信息');
			});
		}

	}; 

	
	function get_record_by_id(id) {
		$.get("task_query.php?action=get&id="+id, function(data){
			eval("var row = "+data);
			$('#task_name').val(row['task_name'])
			$('#free_count').val(row['free_count'])
			$('#dispatch_count').val(row['free_count'])
			init_agent_group();
		})
	}

	function init_agent_group(){
		$.get("agent_query.php?action=get_group_options", function(data) {
			var arr = data.split("$");
			for(var i=0; i<arr.length; i++){
				var kv = arr[i].split(',');
				f.agent_group.options[i+1] = new Option(kv[1], kv[0]);
			}
			$('#btn_save').attr('disabled', false);
			$('#btn_close').attr('disabled', false);
		});
	}

	function agent_group_change(v) {
		$('#agent_td').html("<font style='font-size:12px;color:gray'>数据加载中，请稍候... <img src='loading.gif'></font>");
		$.get("agent_query.php?action=get_agent_by_group&agent_group="+encodeURI(v), function(data) {
			//alert(data)
			var arr = data.split("$");
			var html = []
			for(var i=0; i<arr.length; i++){
				html.push("<input type='checkbox' value='"+arr[i]+"' checked> "+arr[i]+"　");
				if((i+1)%5==0) html.push("<br>")
			}
			$('#agent_td').html(html.join(""));
		});
	}

	function select_all_user(t) {
		$("input[type='checkbox']").each(
			function(){
				$(this).attr('checked', t);
			}	
		);
	}


	$(function() {
		get_record_by_id(task_id); 
		// init_agent_group();
	});
</script>
</body>
</html>